// pages/detail/detail.js
var detailData = require('../../data/localData.js');
var app=getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var detailId = options.indexId; 
    this.setData({
      currentId: detailId
    });
    var currentId=this.data.detailId;
    this.setData({
      dataList:detailData.localData[detailId]
    });
    
    this.setListenerMusic();

    if (app.globalData.gIsPlayingMusic && app.globalData.isCurrentId == detailId){
      this.setData({
        isPlayingMusic:true
      });
    }

    wx.setNavigationBarTitle({
      title: detailData.localData[detailId].name
    })

    var titleStorages = wx.getStorageSync("titleStorages");
    if (titleStorages){
      var currentStorage = titleStorages[detailId];
      this.setData({
        currentStorage: currentStorage
      });
    }else{
      var titleStorages={};
      titleStorages[detailId]=false;
      wx.setStorageSync("titleStorages", titleStorages);
    }
  },
  setListenerMusic:function(){
    var that=this;
    wx.onBackgroundAudioPlay(function(){
      that.setData({
        isPlayingMusic:true
      });
      app.globalData.gIsPlayingMusic=true;
      app.globalData.isCurrentId=this.data.currentId;
      
    })

    wx.onBackgroundAudioPause(function(){
      that.setData({
        isPlayingMusic:false
      });
      app.globalData.gIsPlayingMusic=false;
      app.globalData.isCurrentId = null;
    })
  },


  playMusic:function(){
    var music = detailData.localData[this.data.currentId].music;
    if(this.data.isPlayingMusic){
      wx.stopBackgroundAudio();
      this.setData({
        isPlayingMusic: false
      });
    }else{
      wx.playBackgroundAudio({
        dataUrl: music.musicUrl,
        title: music.musicTitle
      });
      this.setData({
        isPlayingMusic: true
      });
    }
  },
  showCharacter:function(){
    var titleStorages=wx.getStorageSync("titleStorages");
    var currentStorage=titleStorages[this.data.currentId];
    currentStorage=!currentStorage;
    titleStorages[this.data.currentId]=currentStorage;
    wx.setStorageSync('titleStorages', titleStorages);
    this.setData({
      currentStorage:currentStorage
    });
    var title=detailData.localData[this.data.currentId].title;
    var title01 = detailData.localData[this.data.currentId].title01;

    wx.showToast({
      title: currentStorage ? title01 : title,
      icon:'success',
      duration:2000
    })
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})